function addCard(room, deptId) {
	const dept = room.getDept(deptId);
	if (!dept) return;
	let has = false;
	Object.values(dept.handCards).forEach((card) => {
		if (card.cardId == 'NextTurn') has = true;
	});
	if (!has) {
		const card = CardRegistry.get('NextTurn');
		if (card) dept.handCards.unshift(card);
	}
}

// 回合开始时检测手牌中有没有回合结束卡牌，如果没有则添加一个回合结束卡牌。
events.on('gameStart', (room) => {
	Object.values(room.depts).forEach((dept) => {
		addCard(room, dept.deptId);
	});
});
events.on('turnStart', addCard);
events.on('turnEnd', addCard);

GameEndRegistry.register('no_stability', (room) => {
	var dept = room.getDept('xing');
	if (!dept) return null;
	if (dept.getDeptDataValue('stability') <= 0) {
		return '全员游戏失败 - 失败原因：王朝崩溃';
	}
	return null;
});

GameEndRegistry.register('no_population', (room) => {
	var dept = room.getDept('hu');
	if (!dept) return null;
	if (dept.getDeptDataValue('population') <= 0) {
		return '全员游戏失败 - 失败原因：人口耗尽';
	}
	return null;
});

function getDrawCount(room) {
	const dept = room.getDept('bing');
	if (!dept) return 1;
	return Math.max(Math.floor(dept.getDeptDataValue('stength') / 25), 1);
}

// 当国力足够强，获得的卡牌也会更多。
events.on('turnStart', (room, deptId) => {
	const count = getDrawCount(room);
	for (let i = 0; i < count; i++) {
		const card = room.pools.draw(deptId, true);
		if (card) CardEvent.addCard(room, deptId, card);
	}
});
